package com.example.victory.common.web;

import com.example.victory.filter.SpringBootFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;

/**
 * Web请求辅助类
 */
public class WebHelper {
    public static final String SESSIONID_HEADER = "sessionid";
    public static final String REQEUST_ID_HEADER = "requestid";

    private static final Logger loggerFilter = LoggerFactory.getLogger(SpringBootFilter.class);

    /**
     * 获取Http请求对象
     *
     * @return
     */
    public static HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        } else {
            return requestAttributes.getRequest();
        }
    }

    /**
     * 获取Http返回对象
     *
     * @return
     */
    public static HttpServletResponse getHttpServletResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        } else {
            return requestAttributes.getResponse();
        }
    }

    /**
     * 获取会话id，仅从request中获取
     *
     * @return 返回会话id
     */
    public static String getSessionIdFromRequest() {
        HttpServletRequest request = getHttpServletRequest();
        return getSessionId(request, null);
    }

    /**
     * 获取会话id，先从request中获取，找不到再从response中获取
     *
     * @return 返回会话id
     */
    public static String getSessionId() {
        HttpServletRequest request = getHttpServletRequest();
        HttpServletResponse response = getHttpServletResponse();

        return getSessionId(request, response);
    }

    /**
     * 从header中获取sessionid
     *
     * @param httpRequest
     * @param httpResponse
     * @return
     */
    public static String getSessionId(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
        if (httpRequest == null)
            return null;

        String sessionid = httpRequest.getHeader(SESSIONID_HEADER);
        if (StringUtils.isEmpty(sessionid) && httpResponse != null) {
            sessionid = httpResponse.getHeader(SESSIONID_HEADER);
            return sessionid;
        }
        return sessionid;
    }

    /**
     * 从header中获取requestid
     *
     * @param httpRequest
     * @param httpResponse
     * @return
     */
    public static String getRequestId(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
        if (httpRequest == null)
            return null;

        String id = httpRequest.getHeader(REQEUST_ID_HEADER);
        if (StringUtils.isEmpty(id) && httpResponse != null) {
            id = httpResponse.getHeader(REQEUST_ID_HEADER);
            return id;
        }
        return id;
    }

    /**
     * 打印请求结束日志
     *
     * @param result
     */
    public static void logEnd(String result) {
        HttpServletRequest httpRequest = getHttpServletRequest();
        HttpServletResponse httpResponse = getHttpServletResponse();
        String sessionid = getSessionId(httpRequest, httpResponse);
        String requestId = WebHelper.getRequestId(httpRequest, httpResponse);

        // 打印请求信息
        StringBuilder sb = new StringBuilder();
        String info = MessageFormat.format("请求结束:\n请求信息:\n\t网址: {0}\n\t方法: {1}\n\t参数: {2}\n\t远程主机: {3}\n\t会话id: {4}\n\t请求id: {5}", httpRequest.getRequestURL(), httpRequest.getMethod(), httpRequest.getQueryString(), httpRequest.getRemoteHost(), sessionid, requestId);

        sb.append(info).append("\n").append("请求结果:\n").append(result);

        loggerFilter.info(sb.toString());
    }
}
